package com.rme.main.api.impl;

import com.rme.common.core.api.BaseApiService;
import com.rme.common.core.api.BaseResponse;
import com.rme.common.core.utils.RedisUtil;
import com.rme.common.core.utils.ZyhBeanUtils;
import com.rme.main.api.UserInfoService;
import com.rme.main.api.dto.UserResDto;
import com.rme.main.api.impl.entity.SysUser;
import com.rme.main.api.impl.mapper.SysUserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 周益和
 * @ClassName UserInfoServiceImpl
 * @qq 1552951165
 */
@RestController
public class UserInfoServiceImpl extends BaseApiService<UserResDto> implements UserInfoService {
    @Autowired
    private SysUserMapper sysUserMapper;
    /**
     * 根据用户的token获取redis中userId
     * 再根据userId查询db的信息
     * @param token
     * @return
     */
    @Override
    public BaseResponse<UserResDto> getByTokenUserInfo(String token) {
        if (StringUtils.isEmpty(token)){
            return setResultError("token为空");
        }
        // 根据用户的token获取redis中的userid
        String redisUserId = RedisUtil.getString(token);
        if (StringUtils.isEmpty(redisUserId)) {
            return setResultError("token错误");
        }
        int userId = Integer.parseInt(redisUserId);
        SysUser sysUser = sysUserMapper.selectById(userId);
        if (sysUser == null) {
            return setResultError("找不到该用户");
        }
        // 获取db对象 将db中转换成dto
        UserResDto userResDto = ZyhBeanUtils.doToDto(sysUser, UserResDto.class);
        return setResultSuccessData(userResDto);
    }
    
}
